﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CM.BL.BC;
using CM.BL.BE;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace WebAppHTML5
{
    public partial class release : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pRetval = "";
            if (!Request.HttpMethod.Equals("POST"))
            {

            }
            else
            {
                pRetval = "{\"success\":{0}, \"errors\":{\"reason\": \"{1}\"},  ";
                pRetval += "\"oLib\": \"{2}\" ";
                pRetval += "}";
                string _retval = "";
                bool result = releasex(out _retval);
                if (result)
                {
                    pRetval = pRetval.Replace("{0}", "true");
                    pRetval = pRetval.Replace("{1}", _retval);
                    pRetval = pRetval.Replace("{2}", result.ToString());
                }
                else
                {
                    pRetval = pRetval.Replace("{0}", "false");
                    pRetval = pRetval.Replace("{1}", _retval);
                    pRetval = pRetval.Replace("{2}", result.ToString());
                }
            }

            Response.ContentType = "application/json; charset=UTF-8";
            Response.Write(pRetval);
        }

        private bool releasex(out string _retval)
        {
            string strsp, strco, strpos, stritems, retval = ""; bool oLib = false;
            if (Request.Params["Header"].ToString() == "I")
            {
                strsp = Request.Params["SP"].ToString();
                strco = Request.Params["LibCod"].ToString();
                strpos = Request.Params["Posicion"].ToString();

                if (!string.IsNullOrEmpty(strsp) && !string.IsNullOrEmpty(strco) && !string.IsNullOrEmpty(strpos))
                {
                    CredencialesController.cargarInfo(Request.Params); //(SIEMPRE) @@@
                    string SesID = "";
                    if (string.IsNullOrEmpty(Request.Params["Id"]))
                    {
                        SesID = Session.SessionID;
                    }
                    else
                    {
                        SesID = Request.Params["Id"];
                    }
                    //************************************************************* @@@
                    retval = SolicitudPedidoBC.getInstance(SesID).releaseSP(strsp, strco, strpos, out oLib);                
                }
            }
            else if (Request.Params["Header"].ToString() == "H")
            {
                stritems = Request.Params["itemsSP"].ToString() != null ? Request.Params["itemsSP"].ToString() : "";

                if (!string.IsNullOrEmpty(stritems))
                {
                    CredencialesController.cargarInfo(Request.Params); //(SIEMPRE) @@@
                    string SesID = "";
                    if (string.IsNullOrEmpty(Request.Params["Id"]))
                    {
                        SesID = Session.SessionID;
                    }
                    else
                    {
                        SesID = Request.Params["Id"];
                    }
                    //************************************************************* @@@
                    //recuperamos la lista enviada desde sencha y la transformamos a una lista items
                    List<POItemBE> lstItems = JsonConvert.DeserializeObject<List<POItemBE>>(stritems);
                    List<ResultReleaseFromHeader> oLibH = new List<ResultReleaseFromHeader>();
                    retval = SolicitudPedidoBC.getInstance(SesID).releaseSPFromHeader(lstItems, out oLibH);
                    if (validaResult(oLibH))
                    {
                        oLib = true;
                        //leemos la lista oLibH con los resultados de la liberación
                        if (lstItems != null)
	                    {
                	        retval = "Pedido " + lstItems[0].PO + " ";
                            foreach (ResultReleaseFromHeader item in oLibH)
                            {
                                retval += " - " + item.Mensaje + " ";
                            }
	                    }
                    }
                    else
                    {
                        oLib = false;
                        //leemos la lista oLibH con los resultados de la liberación
                        if (lstItems != null)
                        {
                            retval = "Pedido " + lstItems[0].PO;
                            foreach (ResultReleaseFromHeader item in oLibH)
                            {
                                retval += " - " + item.Mensaje + " ";
                            }
                        }
                    }
                   
                }
            }
            _retval = retval;
            return oLib;
            
        }

        private bool validaResult(List<ResultReleaseFromHeader> lstI)
        {
            bool result = true;
            foreach (ResultReleaseFromHeader item in lstI)
            {
                if (item.pLiberado == false)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
    }
}
